<?php
class PNBrand extends PNObjectEx {
  function PNBrand($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'icarrent_brand';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';

    $this->_init($init, $where);
  }

  function validate(){
    $form = FormUtil::getPassedValue ('form', false);
    if (empty($form['name'])){
      $message[] = 'field name must not be empty.';
      $this->setError($message);
      return false;
    }
    $this->clearError();
    return true;
  }

  function insertPostProcess(){

    $sb = FormUtil::getPassedValue ('sb', false);
    $id = $this->_objData['id'];
    if($sb && $id){
      foreach($sb as $key=>$val ){
        //$valArray = explode("@", $key);
        $obj[] = array('category_id'    => $val,
                       'subcategory_id' => $key,
                       'brand_id'       => $id
                      );
      }
      //Do the insert
      DBUtil::insertObjectArray($obj, 'icarrent_subcategory_brand');
    }

  }

  function updatePostProcess(){

    $id = $this->_objData['id'];
    $sbFormArray = FormUtil::getPassedValue ('sb', false);
    if($id && $sbFormArray){
      $sbDBArray = DBUtil::selectObjectArray( 'icarrent_subcategory_brand', 
                                                                "WHERE sbra_brand_id = '$id'",
                                                                '',
                                                                -1,
                                                                -1,
                                                                '',
                                                                null,
                                                                null,
                                                                array( 'id', 
                                                                      'category_id', 
                                                                      'subcategory_id', 
                                                                      'brand_id'
                                                                    )
                                                                );
      //var_dump($sbFormArray);
      //var_dump($sbDBArray);

      if(sizeof($sbDBArray)>0){
        foreach($sbFormArray as $keyForm=>$valForm ){
          $compare = false;
          foreach($sbDBArray as $keyDB=>$valDB ){
            if($keyForm == $valDB['subcategory_id']){
              $compare = true;
              $res[$keyForm] = 'equal';
            }
          }
          if($compare == false) {
            //Insert state when exist data
            //$res[$keyForm] = 'not equal';
            $obj = array('category_id'    => $valForm,
                           'subcategory_id' => $keyForm,
                           'brand_id'       => $id
                          );
            //Do the insert
            DBUtil::insertObject($obj, 'icarrent_subcategory_brand');
          }
        }
      }else{
        //Insert new data statement when data is empty
        foreach($sbFormArray as $keyForm=>$valForm ){
          $obj = array('category_id'    => $valForm,
                           'subcategory_id' => $keyForm,
                           'brand_id'       => $id
                          );
          //Do the insert
          DBUtil::insertObject($obj, 'icarrent_subcategory_brand');
        }
      }
      //var_dump($res); exit; 

      foreach($sbDBArray as $keyDB=>$valDB ){
        $compare = false;
        foreach($sbFormArray as $keyForm=>$valForm ){
          if($keyForm == $valDB['subcategory_id']){
            $compare = true;
            $db[$valDB['subcategory_id']] = 'equal';
          }
        }
        if($compare == false) {
            //Delete state when database exist
            //$db[$valDB['subcategory_id']] = 'not equal';
            DBUtil::deleteObjectByID( 'icarrent_subcategory_brand', $valDB['id'], 'id');
        }
      }
    }
    //var_dump($db); exit;
  }



  function deletePostProcess(){
    $id = $this->_objData['id'];
    //Delete old data 
    DBUtil::deleteObjectByID('icarrent_subcategory_brand', $id, 'brand_id');
  }


  function selectExtendResult(){
    $id = FormUtil::getPassedValue ('id', false);
    $result = array();
    if ($id){

      $result['subcategory_brand'] = DBUtil::selectObjectArray( 'icarrent_subcategory_brand', 
                                                                "WHERE sbra_brand_id = '$id'",
                                                                '',
                                                                -1,
                                                                -1,
                                                                '',
                                                                null,
                                                                null,
                                                                array( 'id', 
                                                                      'category_id', 
                                                                      'subcategory_id', 
                                                                      'brand_id'
                                                                    )
                                                                );

    }
    return $result;
  }
}